using System;
using System.Xml;

namespace Mbs.CastleContainer
{
    class IfElementProcessor : AbstractStatementElementProcessor
    {
        public IfElementProcessor()
        {
        }

        public override String Name
        {
            get { return "if"; }
        }

        public override void Process(IXmlProcessorNodeList nodeList, IXmlProcessorEngine engine)
        {
            XmlElement element = nodeList.Current as XmlElement;

            bool processContents = ProcessStatement(element, engine);

            if (processContents)
            {
                XmlDocumentFragment fragment = CreateFragment(element);
                MoveChildNodes(fragment, element);
                engine.DispatchProcessAll(new DefaultXmlProcessorNodeList(fragment.ChildNodes));
                ReplaceItself(fragment, element);
            }
            else
            {
                RemoveItSelf(element);
            }
        }
    }
}
